package com.hc.exer;

import com.hc.Employee;
import org.junit.Test;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;

public class TestLambda {


    /**
     * 测试集合代码(公用)
     * ------------------------------------------------------------------------
     */
    List<Employee> employees = Arrays.asList(
            new Employee("张三", 18, 9999.99),
            new Employee("李四", 38, 9000.11),
            new Employee("王五", 50, 9111.22),
            new Employee("赵六", 26, 9222.33),
            new Employee("田七", 17, 9999.33)
    );

    /**
     * ------------------------------------------------------------------------
     */


    /**
     * 按年龄从小到大排序
     */
    @Test
    public void test1   () {
        Collections.sort(employees, (e1, e2) -> {
            if (e1.getAge() == e2.getAge()) {
                return e1.getName().compareTo(e2.getName());
            } else {
                return Integer.compare(e1.getAge(), e2.getAge());    //从大到下可以在Integer前加-号
            }
        });

        for (Employee emp : employees) {
            System.out.println(emp);
        }
    }


    /**
     * 需求:用于处理字符串
     */


    @Test
    public void  test2(){
        String trimStr = strHandler("\t\t\t 输出:加油努力", (str) -> str.trim());
        System.out.println(trimStr);

        //字符串转大写
        String upper = strHandler("abcdef", String::toUpperCase);
        System.out.println(upper);

        String handler = strHandler("输出:加油努力", (str) -> str.substring(3, 7));
        System.out.println(handler);
    }

    public String strHandler(String str,MyFunction mf){
        return mf.getValue(str);
    }


    /**
     * 需求:对于两个Long 型数据进行处理
     */
    @Test
    public void test3(){
        op(100L,200L,(x,y) -> x+y);

        op(100L,200L,(x,y) -> x*y);
    }




    public void op(Long l1,Long l2,MyFunction2<Long,Long> mf){
        System.out.println(mf.getValue(l1,l2));

    }

}
